/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package data;

import business.Historico;

import java.util.GregorianCalendar;
import java.util.ArrayList;
import java.sql.SQLException;
import java.sql.ResultSet;

/**
 *
 * @author Tiago
 */
public class HistoricoData extends InterfaceBD {

    public HistoricoData() {
        super();
    }

    
    public void adicionarHistorico(Historico h) throws SQLException {
        int codH = getnextCodHist();
        String des = h.getDescricao();
        int codc = h.getCodCliente();
        GregorianCalendar hora = h.getData();
        int dia =  hora.get(GregorianCalendar.DAY_OF_MONTH)+1;
        int mes = hora.get(GregorianCalendar.MONTH)+1;
        int ano = hora.get(GregorianCalendar.YEAR);
        String dataS = ano+"-"+mes+"-"+dia;
        String query = "Insert INTO TABLE Historico Values("+codH+","+codc+",'"+dataS+"','"+des+"')";
        System.out.println(query);
        res= st.executeUpdate(query);


    }

    public ArrayList<Historico> consultarHistoricoCliente(int codCliente) throws SQLException{
        ArrayList<Historico> resultado = new ArrayList<Historico>();
        String query = "Select = from Historico where cod_uh="+codCliente;
        System.out.println(query);
        rs=st.executeQuery(query);
        while(rs.next()){
            GregorianCalendar data = new GregorianCalendar();
            data.setTime(rs.getDate(3));
            resultado.add(new Historico(rs.getString(4),data,codCliente));
        }
        return resultado;
    }

    public int getnextCodHist() throws SQLException {

        ResultSet rs;
        int num = 0;

        try {
            rs = st.executeQuery("SELECT MAX(cod_hist) FROM Historico");
            rs.next();
            num = rs.getInt(1);
        }
        catch (SQLException e) {

            //
        }
        return(num+1);
    }

}
